jQuery ->
  el = $('#stock_movement_stock_item_id')
  if el.length
    el.select2
      placeholder: "Find a stock item" # translate
      ajax:
        url: Spree.routes.stock_items_api(el.data('stock-location-id'))
        params: { "headers": { "X-Spree-Token": Spree.api_key } }
        data: (term, page) ->
          q:
            variant_product_name_cont: term
          per_page: 50
          page: page
          token: Spree.api_key
        results: (data, page) ->
          more = (page * 50) < data.count
          return { results: data.stock_items, more: more }
      formatResult: (stock_item) ->
        variantTemplate({ variant: stock_item.variant })
      formatSelection: (stock_item, container, excapeMarkup) ->
        Select2.util.escapeMarkup("#{stock_item.variant.name} (#{stock_item.variant.options_text})")
